﻿var muser = {
    //列表初始化
    gridInit: function () {
        $("#muser-grid").datagrid({
            url: "/manage/user/grid",
            nowrap: false, rownumbers: true, border: false,
            fit: true, singleSelect: true,
            pageSize: 20, pagination: true,
            idField: "ID", toolbar: "#muser-tools",
            sortName: "InsertTime", sortOrder: "desc",
            columns:
            [[
                { field: "UserName", title: "账号", width: 140, align: "center" },
                { field: "Name", title: "姓名", width: 80, align: "center" },
                { field: "Mail", title: "邮箱", width: 180, align: "center" },
                { field: "Remark", title: "描述", width: 200, align: "center" },
                { field: "InsertTime", title: "录入时间", width: 140, align: "center", sortable: true },
                {
                    field: "IsEnabled", title: "启用", width: 50, align: "center", sortable: true,
                    formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                }
            ]],
            onSelect: function (index, row) {
                $("#muserdepartmentpost-grid").datagrid("unselectAll");
                $("#muserdepartmentpost-grid").datagrid("load", { userid: row.ID });

                $("#muserrole-grid").datagrid("unselectAll");
                $("#muserrole-grid").datagrid("load", { userid: row.ID });
            }
        });
    },
    //搜索框初始化
    searchInit: function () {
        $("#muser-search").searchbox({
            width: 150, prompt: "请输入账号或姓名查询",
            searcher: function (value) {
                $("#muser-grid").datagrid("unselectAll");
                $("#muser-grid").datagrid("load", { search: value });

                $("#muserdepartmentpost-grid").datagrid("unselectAll");
                $("#muserdepartmentpost-grid").datagrid("loadData", []);
                delete $("#muserdepartmentpost-grid").datagrid("options").queryParams;

                $("#muserrole-grid").datagrid("unselectAll");
                $("#muserrole-grid").datagrid("loadData", []);
                delete $("#muserrole-grid").datagrid("options").queryParams;
            }
        });
    },

    //添加
    insert: function () {
        muser.mutual("添加", null);
    },
    //修改
    update: function () {
        var row = $("#muser-grid").datagrid("getSelected");
        if (row) {
            muser.mutual("修改", row.ID);
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //删除
    remove: function () {
        var row = $("#muser-grid").datagrid("getSelected");
        if (row) {
            manage.confirm("确认删除?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/user/remove",
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) { muser.reload(); }
                        }
                    });
                }
            });
        }
        else { manage.slide(manage.delNullRowMsg); }
    },
    //实体操作
    mutual: function (title, id) {
        var href = "/manage/user/page?" + $.param({ id: id });
        var d = manage.dialog({
            title: title, href: href, width: 740,
            handler: function () {
                manage.form({
                    dialog: d, formid: "#muser-form",
                    url: "/manage/user/save",
                    success: function (result) { muser.reload(); }
                });
            },
            onLoad: function () { muser.formInit(id); }
        });
    },
    //弹窗初始化
    formInit: function (id) {
        if (id) {
            $("#muser-password").remove();
            $("#muser-username").textbox("disable");
        }
    },

    //启用
    enable: function () {
        muser.enableOrDisable(true);
    },
    //禁用
    disable: function () {
        muser.enableOrDisable(false);
    },
    //启用禁用
    enableOrDisable: function (bool) {
        var row = $("#muser-grid").datagrid("getSelected");
        if (row) {
            manage.ajax({
                url: "/manage/user/enableordisable",
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    manage.slide(result.message);
                    if (result.success) {
                        $("#muser-grid").datagrid("reload");
                    }
                }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },

    //刷新列表
    reload: function () {
        $("#muser-grid").datagrid("reload");
        $("#muser-grid").datagrid("unselectAll");

        $("#muserdepartmentpost-grid").datagrid("unselectAll");
        $("#muserdepartmentpost-grid").datagrid("loadData", []);
        delete $("#muserdepartmentpost-grid").datagrid("options").queryParams;

        $("#muserrole-grid").datagrid("unselectAll");
        $("#muserrole-grid").datagrid("loadData", []);
        delete $("#muserrole-grid").datagrid("options").queryParams;
    },

    //重置密码
    reset: function () {
        var row = $("#muser-grid").datagrid("getSelected");
        if (row) {
            var d = manage.dialog({
                title: "重置密码", width: 740,
                href: "/manage/user/resetpwd?" + $.param({ id: row.ID }),
                handler: function () {
                    manage.form({
                        dialog: d, formid: "#muser-resetpwd",
                        url: "/manage/user/setresetpwd",
                        success: function (result) { }
                    });
                },
                onLoad: function () { }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    }
};

$(function () { muser.gridInit(); muser.searchInit(); });